package com.alexander.cloundexplorer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alexander.cloundexplorer.adapter.FilesAdapter;
import com.alexander.cloundexplorer.bean.params.LoginParams;
import com.alexander.cloundexplorer.dialog.FileOptionsDialog;
import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.params.QuerySecondFilesParams;
import com.alexander.cloundexplorer.bean.params.User;
import com.alexander.cloundexplorer.bean.response.BaseResponse;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;
import com.alexander.cloundexplorer.fragment.BaseFragment;
import com.alexander.cloundexplorer.fragment.SearchClassifyFileUnderOverallFragment;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * @author Administrator
 * .....
 */
public class ClassifyFilesActivity extends BaseActivity implements FilesAdapter.OnItemClickLisnter, FileOptionsDialog.OnDelLisnter
{
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.fragment_Layout)
    RelativeLayout fragmentLayout;

    private User user = new User();
    private String secondFileTypeId;
    private List<FileInfoResponse> list = new ArrayList<>();
    private FilesAdapter filesAdapter;
    private QuerySecondFilesParams querySecondFilesParams = new QuerySecondFilesParams();
    private FileOptionsDialog fileOptionsDialog;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_files);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        Intent intent = getIntent();
        user = (User) intent.getExtras().getSerializable("user");
        secondFileTypeId = intent.getStringExtra("id");
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        filesAdapter = new FilesAdapter(this, list);
        filesAdapter.setOnItemClickLisnter(this);
        recyclerView.setAdapter(filesAdapter);

        querySecondFilesParams.setAccount(user.getPhone());
        querySecondFilesParams.setPassword(user.getPassword());
        querySecondFilesParams.setSecondFileTypeId(secondFileTypeId);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        OkHttpUtils
                .post()
                .url(Constants.IP.concat("/fileController/querySecondFiles"))
                .addParams("params", JSON.toJSONString(querySecondFilesParams))
                .build()
                .execute(new StringCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        showTip(e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id)
                    {
                        showTip(response);
                        BaseResponse baseResponse = JSON.parseObject(response, BaseResponse.class);
                        list.removeAll(list);
                        list.addAll(JSON.parseArray(baseResponse.getData().toString(), FileInfoResponse.class));
                        filesAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_options_classifyfile, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.search:
                BaseFragment baseFragment = SearchClassifyFileUnderOverallFragment.getInstance(secondFileTypeId, new LoginParams(user.getPhone(), user.getPassword()));
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_Layout, baseFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                return true;
            case R.id.root:
                return true;
            case R.id.trash:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onItemClick(int position)
    {
        showTip("打开文件".concat(list.get(position).getFileName().concat(list.get(position).getFile_postfix())));
    }

    @Override
    public void onMoreClick(int position)
    {
        showTip("更多");
        fileOptionsDialog = new FileOptionsDialog(ClassifyFilesActivity.this, list.get(position), user);
        fileOptionsDialog.setOnDelLisnter(ClassifyFilesActivity.this);
        fileOptionsDialog.show();
    }

    /**
     * 文件删除成功失败的回调
     */
    @Override
    public void onDelSuccess(String result, FileInfoResponse fileInfoResponse)
    {
        list.remove(fileInfoResponse);
        filesAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDelError(Throwable throwable)
    {
        showTip(throwable.getMessage());
    }
}